﻿using DBUtil;
using Function;
using Model.Permission;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL.Permission.Shop
{
    public class ExtendedRoleAuthorityDAL
    {
        DbHelperSQLP DbHelperSQL = new DbHelperSQLP(PubConstant.JHShop);
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool HavePermission(string message, int userid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM Permission.ExtendedPermission t1 INNER JOIN Permission.ExtendedRoleAuthority t2 ON t1.ExtendedPermissionID = t2.PermissionId WHERE t1.CustomMessage = @message AND t2.RoleId IN (SELECT RoleID FROM Permission.UserRoleAssign t3 WHERE t3.UserID = @userid)");
            SqlParameter[] parameters = {
					new SqlParameter("@message", message),
                    new SqlParameter("@userid", userid),
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool HavePermission(int rightID, int userid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM Permission.ExtendedRoleAuthority t2 where t2.PermissionId=@rightid t2.RoleId IN (SELECT RoleID FROM Permission.UserRoleAssign t3 WHERE t3.UserID = @userid)");
            SqlParameter[] parameters = {
					new SqlParameter("@rightid", rightID),
                    new SqlParameter("@userid", userid),
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ExtendedRoleAuthorityID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[ExtendedRoleAuthority]");
            strSql.Append(" where ExtendedRoleAuthorityID=@ExtendedRoleAuthorityID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExtendedRoleAuthorityID", SqlDbType.Int,4)
			};
            parameters[0].Value = ExtendedRoleAuthorityID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ExtendedRoleAuthorityEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Permission].[ExtendedRoleAuthority](");
            strSql.Append("RoleId,PermissionId)");
            strSql.Append(" values (");
            strSql.Append("@RoleId,@PermissionId)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleId", SqlDbType.Int,4),
					new SqlParameter("@PermissionId", SqlDbType.Int,4)};
            parameters[0].Value = model.RoleId;
            parameters[1].Value = model.PermissionId;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ExtendedRoleAuthorityEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [Permission].[ExtendedRoleAuthority] set ");
            strSql.Append("RoleId=@RoleId,");
            strSql.Append("PermissionId=@PermissionId");
            strSql.Append(" where ExtendedRoleAuthorityID=@ExtendedRoleAuthorityID");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleId", SqlDbType.Int,4),
					new SqlParameter("@PermissionId", SqlDbType.Int,4),
					new SqlParameter("@ExtendedRoleAuthorityID", SqlDbType.Int,4)};
            parameters[0].Value = model.RoleId;
            parameters[1].Value = model.PermissionId;
            parameters[2].Value = model.ExtendedRoleAuthorityID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int RoleId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[ExtendedRoleAuthority] ");
            strSql.Append(" where RoleId=@RoleId");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleId", SqlDbType.Int,4)
			};
            parameters[0].Value = RoleId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        public bool Delete(int Roleid, string mtid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Permission.ExtendedRoleAuthority ");
            strSql.Append(" where RoleID=@RA_RID and [PermissionId] IN (SELECT [ExtendedPermissionID] FROM [Permission].[ExtendedPermission] WHERE TypeId=@mtid)");
            SqlParameter[] parameters = {
					new SqlParameter("@RA_RID",Roleid),
                    new SqlParameter("@mtid", mtid)
			};

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ExtendedRoleAuthorityIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[ExtendedRoleAuthority] ");
            strSql.Append(" where ExtendedRoleAuthorityID in (" + ExtendedRoleAuthorityIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ExtendedRoleAuthorityEntity GetModel(int ExtendedRoleAuthorityID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ExtendedRoleAuthorityID,RoleId,PermissionId from [Permission].[ExtendedRoleAuthority] ");
            strSql.Append(" where ExtendedRoleAuthorityID=@ExtendedRoleAuthorityID");
            SqlParameter[] parameters = {
					new SqlParameter("@ExtendedRoleAuthorityID", SqlDbType.Int,4)
			};
            parameters[0].Value = ExtendedRoleAuthorityID;

            ExtendedRoleAuthorityEntity model = new ExtendedRoleAuthorityEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ExtendedRoleAuthorityEntity DataRowToModel(DataRow row)
        {
            ExtendedRoleAuthorityEntity model = new ExtendedRoleAuthorityEntity();
            if (row != null)
            {
                if (row["ExtendedRoleAuthorityID"] != null && row["ExtendedRoleAuthorityID"].ToString() != "")
                {
                    model.ExtendedRoleAuthorityID = int.Parse(row["ExtendedRoleAuthorityID"].ToString());
                }
                if (row["RoleId"] != null && row["RoleId"].ToString() != "")
                {
                    model.RoleId = int.Parse(row["RoleId"].ToString());
                }
                if (row["PermissionId"] != null && row["PermissionId"].ToString() != "")
                {
                    model.PermissionId = int.Parse(row["PermissionId"].ToString());
                }
            }
            return model;
        }

    }
}
